package servlets;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import companySupSession.CompanySupSession;

import descriptor.CompanyDescriptor;
import descriptor.CompanySupDescriptor;
import descriptor.DegreeDescriptor;
import descriptor.DegreeListDescriptor;
import descriptor.FeedbackDescriptor;
import descriptor.StudentDescriptor;
import descriptor.StudentListDescriptor;

public class CompanySupServlet extends HttpServlet{
	
	
	protected void doPostAndGet(HttpServletRequest req ,HttpServletResponse res)
	throws IOException, ServletException{
		
		try{
			Context initialContext = null;
			try {
				initialContext = new InitialContext() ;
			} catch (NamingException e) {
				e.printStackTrace();
			}
			HttpSession session = req.getSession(true);
			CompanySupSession sessionBean = (CompanySupSession)session.getAttribute("companySupSession");
			
			
			RequestDispatcher dispatcher = null ;
			if (!(sessionBean.logged())){
				dispatcher = req.getRequestDispatcher("./companysupervisor/work/compsup_notlogged.jsp") ;
				dispatcher.forward(req, res) ;
				return;
			}
			String action = req.getParameter("action") ;
			if(sessionBean == null){
				dispatcher = req.getRequestDispatcher("index.jsp") ;
				dispatcher.forward(req, res) ;
				return;
			}
			if (action == null){
				CompanySupDescriptor perso = sessionBean.getCompanySupDescriptor();
				req.setAttribute("perso",perso);
				dispatcher = req.getRequestDispatcher("./companysupervisor/work/compsup_index.jsp") ;
				dispatcher.forward(req, res) ;
			}else if (action.equals("logout")){
				sessionBean.logout();
				dispatcher = req.getRequestDispatcher("/") ;
				dispatcher.forward(req, res) ;
				return;
			}else if (action.equals("perso_more")){
				CompanySupDescriptor perso = sessionBean.getCompanySupDescriptor();
				CompanyDescriptor company = sessionBean.getCompanyDescriptor(perso.getCompanyId());
				req.setAttribute("perso",perso);
				req.setAttribute("company",company);
				dispatcher = req.getRequestDispatcher("./companysupervisor/work/compsup_perso_infos.jsp") ;
				dispatcher.forward(req, res) ;
			}else if (action.equals("perso_update")){
				String infos = "";
				CompanySupDescriptor perso = sessionBean.getCompanySupDescriptor();
				String email = req.getParameter("email");
				String oldPassword = req.getParameter("oldPassword");
				String newPassword1 = req.getParameter("newPassword1");
				String newPassword2 = req.getParameter("newPassword2");
				if ((email != null)&&(oldPassword != null)&&(newPassword1 != null)&&(newPassword2 != null)){
					int resultCode = 0;
					if (newPassword1 != ""){
						if (!newPassword1.equals(newPassword2)){
							resultCode = -3;
							}else{
							resultCode = sessionBean.editInfos(oldPassword,newPassword1,email);
						}
					}else{
						resultCode = sessionBean.editInfos(null,null,email);
					}
					if (resultCode == 0){
						infos += "<p>Profile successfully updated.</p>";
						perso = sessionBean.getCompanySupDescriptor();
					}else if (resultCode == -1){
						infos += "<p>ERROR: You are not logged.</p>";
					}else if (resultCode == -2){
						infos += "<p>ERROR: You provided a wrong old password.</p>";
					}else if (resultCode == -3){
						infos += "<p>ERROR: You should provide the same new password and confirm new password.</p>";
					}
				}
				req.setAttribute("infos",infos);
				req.setAttribute("perso",perso);
				dispatcher = req.getRequestDispatcher("./companysupervisor/work/compsup_perso_update.jsp") ;
				dispatcher.forward(req, res) ;
			}else if (action.equals("degrees_viewall")){
				DegreeListDescriptor degrees = sessionBean.getPersonnalDegreeDescriptors();
				req.setAttribute("degrees",degrees);
				dispatcher = req.getRequestDispatcher("./companysupervisor/work/compsup_degrees_list.jsp") ;
				dispatcher.forward(req, res) ;
			}else if (action.equals("degrees_more")){
				String errors = "";
				String degreeIdValue = req.getParameter("degreeId");
				int degreeId = Integer.parseInt(degreeIdValue);
				String infos = req.getParameter("infos");
				if (infos != null){
					int resultCode = sessionBean.editFeedBackOnDegree(degreeId,infos);
					errors +="<p>result code "+resultCode+"</p>";
				}
				DegreeDescriptor degree = sessionBean.getDegreeDescriptor(degreeId);
				FeedbackDescriptor feedback = sessionBean.getLinkedFeedBackOnDegree(degreeId);
				if (feedback == null){
					feedback = new FeedbackDescriptor(0,"","",0,"");
				}
				req.setAttribute("errors",errors);
				req.setAttribute("degree",degree);
				req.setAttribute("feedback",feedback);
				dispatcher = req.getRequestDispatcher("./companysupervisor/work/compsup_degrees_more.jsp") ;
				dispatcher.forward(req, res) ;
			}else if (action.equals("students_add")){
				String infos = "";
				String studentIdValue = req.getParameter("studentId");
				if(studentIdValue != null){
					try{
						int studentId = Integer.parseInt(studentIdValue);
						int resultCode = sessionBean.addAStudent(studentId);
						if (resultCode == 0){
							infos += "<p>Student successfully added.</p>";
						}else{
							infos += "<p>ERROR : error code: "+resultCode+".</p>";
						}
						
					}catch (NumberFormatException nfe){
						infos += "<p>ERROR : id provided :"+studentIdValue+" is not valid.</p>";
					}
				}
				req.setAttribute("infos",infos);
				dispatcher = req.getRequestDispatcher("./companysupervisor/work/compsup_students_add.jsp") ;
				dispatcher.forward(req, res) ;
			}else if (action.equals("students_more")){
				String studentIdValue = req.getParameter("studentId");
				int studentId = Integer.parseInt(studentIdValue);
				String infos = req.getParameter("infos");
				if (infos != null){
					sessionBean.editFeedBackOnStudent(studentId,infos);
				}
				StudentDescriptor student = sessionBean.getStudentDescriptor(studentId);
				DegreeDescriptor degree = sessionBean.getDegreeDescriptor(student.getDegree());
				FeedbackDescriptor feedback = sessionBean.getLinkedFeedBackOnStudent(studentId);
				if (feedback == null){
					feedback = new FeedbackDescriptor(0,"","",0,"");
				}
				req.setAttribute("student",student);
				req.setAttribute("degree",degree);
				req.setAttribute("feedback",feedback);
				dispatcher = req.getRequestDispatcher("./companysupervisor/work/compsup_students_more.jsp") ;
				dispatcher.forward(req, res) ;
			}else if (action.equals("students_viewall")){
				StudentListDescriptor students = sessionBean.getPersonnalStudentDescriptors();
				req.setAttribute("students",students);
				dispatcher = req.getRequestDispatcher("./companysupervisor/work/compsup_students_list.jsp") ;
				dispatcher.forward(req, res) ;
			}
			
		}catch (Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	
	
	
	
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException{
		doPostAndGet(req,res);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException{
		doPostAndGet(req,res);
	}
}
